package activities;

import fragments.FinishScheduleFragment;
import helper.HttpHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.Session;
import com.matchcalendar.R;

import dataFormat.CalendarTextData;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class FinishScheduleActivity extends FragmentActivity {
	private Button buttonPrevious, buttonDone;
	public final int SET_SCHEDULE_REQUEST_CODE = 0;
	private FinishScheduleFragment finishScheduleFragment = new FinishScheduleFragment(true, null); 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.custom_set_fixed_schedule);
		findView();
		setListenerOrAdapter();
		getSupportFragmentManager().beginTransaction()
		.add(R.id.linearLayoutRoot, finishScheduleFragment).commit();
	}

	private void findView() {
		buttonPrevious = (Button) findViewById(R.id.buttonPrevious);
		buttonDone = (Button) findViewById(R.id.buttonDone);
	}
	
	private void setListenerOrAdapter() {
		buttonPrevious.setOnClickListener(new ButtonPreviousClickListener());
		buttonDone.setOnClickListener(new ButtonDoneClickListener());
	}
	
	private class ButtonPreviousClickListener implements Button.OnClickListener {

		@Override
		public void onClick(View v) {
			FinishScheduleActivity.this.finish();
		}
	}
	
	private class ButtonDoneClickListener implements Button.OnClickListener {

		@Override
		public void onClick(View v) {
			new AlertDialog.Builder(FinishScheduleActivity.this)
			.setTitle("完成行程表")
			.setMessage("已經完成行程表了嗎")
			.setNegativeButton("還沒", null)
			.setPositiveButton("完成了", new FinishScheduleButtonClickListener())
			.show();
		}
		
		private class FinishScheduleButtonClickListener implements DialogInterface.OnClickListener {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				new PostSaveSchedule().execute(getIntent().getExtras().getString("myFBID"));
			}
			
			private class PostSaveSchedule extends AsyncTask<String, Void, Void> {
				private ProgressDialog proDialog;
				
				@Override
				protected void onPreExecute() {
					proDialog = ProgressDialog.show(FinishScheduleActivity.this, "上傳行程表", "正在上傳行程表...");
					proDialog.setCancelable(false);
				}
				
				@Override
				protected Void doInBackground(String... params) {
					try {
						JSONObject jsonObj = new JSONObject();
						JSONArray informsJsonArr = new JSONArray();
						
						jsonObj.put("id", params[0]);
						for (CalendarTextData itData: finishScheduleFragment.getCalendarTextDataSet()) {
							JSONObject informJsonObj = new JSONObject();
							
							informJsonObj.put("day", itData.getDay());
							informJsonObj.put("startHour", itData.getStartHour());
							informJsonObj.put("startMinute", itData.getStartMinute());
							informJsonObj.put("overHour", itData.getOverHour());
							informJsonObj.put("overMinute", itData.getOverMinute());
							informJsonObj.put("title", itData.getTitle());
							informJsonObj.put("ps", itData.getPs());
							informJsonObj.put("priority", itData.getPriority());
							informsJsonArr.put(informJsonObj);
						}
						jsonObj.put("informs", informsJsonArr);
						HttpHelper.executePost("http://iculture.twgg.org/schedule/insertSchedule.php", jsonObj.toString());
					} catch (JSONException e) {
						e.printStackTrace();
					}
					return null;
				}
				
				@Override
				protected void onPostExecute(Void result) {
					setResult(RESULT_OK);
					proDialog.cancel();
					FinishScheduleActivity.this.finish();
				}
			}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case SET_SCHEDULE_REQUEST_CODE:
					Bundle bundle = data.getExtras();
					int startHour = bundle.getBundle("startTime").getInt("hour");
					int startMinute = bundle.getBundle("startTime").getInt("minute");
					int overHour = bundle.getBundle("overTime").getInt("hour");
					int overMinute = bundle.getBundle("overTime").getInt("minute");
					int priority = bundle.getInt("priority");
					String title = bundle.getString("title");
					String ps = bundle.getString("ps");
					
					for (int day: bundle.getIntegerArrayList("dayList")) {
						CalendarTextData calTextData = new CalendarTextData(day, startHour,
								startMinute, overHour, overMinute, priority, title, ps);
						
						finishScheduleFragment.addCalendarText(calTextData, null, null,
								Gravity.CENTER_HORIZONTAL);
					}
					break;
				default:
					Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
			}
		}
	}
}
